#!/usr/bin/env python
import dbus
import dbus.service
import dbus.glib
import gobject
import os
from os.path import join, getsize

#Capturem les cameres que soporten video4linux
def getCamerasV4L():
	cameras=[]
	bus = dbus.SystemBus()
	hal_manager_obj = bus.get_object("org.freedesktop.Hal", "/org/freedesktop/Hal/Manager")
	hal_manager = dbus.Interface(hal_manager_obj, "org.freedesktop.Hal.Manager")
	device_names = hal_manager.GetAllDevices()
	for device in device_names:
		try:
			device_obj = bus.get_object("org.freedesktop.Hal", device)
			category = device_obj.GetProperty("info.category", dbus_interface="org.freedesktop.Hal.Device")
			if (category == "video4linux"):
				path = device_obj.GetProperty("video4linux.device", dbus_interface="org.freedesktop.Hal.Device")
				cameras= cameras + [device+ " "+ path]
		except:
			pass   
	return cameras

#Capturem cameres que no soporten video4linux
def getCameras(directory):
	cameras=[]
	for root, dirs, files in os.walk(directory):
		if (dirs != []):
			for d in dirs:
				value = getCameras(d)
				if (value != None):
					cameras = cameras + value
		else:
			myfile = open(root + "/" + files[0], 'r')
			myfile.readline()
			myfile.readline()
			myfile.readline()
			line = myfile.readline()
			identifier=line.replace('Device Id        :','')
			identifier = identifier.replace('\n','')
			cameras = cameras + [identifier + " " +root + "/" + files[0]]
	return cameras
